package com.boot.down.util.wrapper;

import com.boot.down.util.message.CommonMessage;
import com.boot.down.util.message.ICommonMessage;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * 接口返回结果model
 *
 * @author DELL 2018/10/29 9:40
 **/
@ToString
@Slf4j
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -5808314244744265188L;

    private Result() {
    }

    public Result(T data, int code, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 返回结果对象
     */
    private T data;

    /**
     * 返回code编码
     */
    private int code;

    /**
     * 返回描述
     */
    private String msg;

    public T getData() {
        return data;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }


    /**
     * 判断是否成功
     *
     * @param
     * @return true或false
     **/
    public boolean ifSuccess() {
        return this.code == CommonMessage.OK.getCode();
    }

    /**
     * 返回成功消息
     *
     * @param data 数据
     * @return Result对象
     **/
    public static <T> Result createSuccess(T data) {
        return new Result(data, CommonMessage.OK.getCode(), CommonMessage.OK.getMsg());
    }

    /**
     * 返回成功消息
     * 如果commonMessage对象为空或code编码不是成功的code编码返回OK
     *
     * @param data          数据
     * @param commonMessage 枚举值 {@link ICommonMessage}
     * @return Result对象
     **/
    public static <T> Result createSuccess(T data, ICommonMessage commonMessage) {
        if (commonMessage == null || commonMessage.getCode() != CommonMessage.OK.getCode()) {
            return new Result(data, CommonMessage.OK.getCode(), CommonMessage.OK.getMsg());
        }
        return new Result(data, CommonMessage.OK.getCode(), commonMessage.getMsg());
    }

    /**
     * 返回错误消息
     * 如果commonMessage对象为空或code编码是成功的code编码就返回ERROR
     *
     * @param commonMessage 枚举值 {@link ICommonMessage}
     * @return Result对象
     **/
    public static <T> Result createFailure(ICommonMessage commonMessage) {
        if (commonMessage == null || commonMessage.getCode() == CommonMessage.OK.getCode()) {
            log.warn("创建失败返回的错误信息为null");
            return new Result(CommonMessage.ERROR.getCode(), CommonMessage.ERROR.getMsg());
        }
        return new Result(commonMessage.getCode(), commonMessage.getMsg());
    }

    /**
     * 返回错误消息
     * 如果commonMessage对象为空或code编码是成功的code编码就返回ERROR
     *
     * @param data          data数据
     * @param commonMessage 枚举值 {@link ICommonMessage}
     * @return Result对象
     **/
    public static <T> Result createFailure(T data, ICommonMessage commonMessage) {
        if (commonMessage == null || commonMessage.getCode() == CommonMessage.OK.getCode()) {
            return new Result(CommonMessage.ERROR.getCode(), CommonMessage.ERROR.getMsg());
        }
        return new Result(data, commonMessage.getCode(), commonMessage.getMsg());
    }

}
